package com.wx.freemarker.method;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

/**
 * 判断是否有权限
 */
public class ShiroIsPermitted implements TemplateMethodModelEx {
	@Override
	public Object exec(List arguments) throws TemplateModelException {
		if (arguments == null || arguments.isEmpty()) {
			return false;
		}
		try {
			SimpleScalar args1 = (SimpleScalar) arguments.get(0);
			String arg0 = args1.getAsString();
			Subject o = SecurityUtils.getSubject();
			if(o==null || !o.isAuthenticated()){
				return false;
			}
			if (o.isPermitted(arg0)) {
				return true;
			} else {
				return false; 
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}
